package com.jinx.test;

import com.github.houbb.sisyphus.core.core.RetryWaiter;
import com.github.houbb.sisyphus.core.core.Retryer;
import com.github.houbb.sisyphus.core.support.condition.RetryConditions;
import com.github.houbb.sisyphus.core.support.listen.RetryListens;
import com.github.houbb.sisyphus.core.support.recover.Recovers;
import com.github.houbb.sisyphus.core.support.wait.NoRetryWait;

import java.util.*;
import java.util.concurrent.Callable;

/**
 * @author jixia
 * @date 2021/10/21 16:16
 * @description 返回随机数，并根据概率、比率
 */
public class MathRandom {
    /**
     * 0出现的概率为%50
     */
    public static double rate0 = 0.50;
    /**
     * 1出现的概率为%20
     */
    public static double rate1 = 0.20;
    /**
     * 2出现的概率为%15
     */
    public static double rate2 = 0.15;
    /**
     * 3出现的概率为%10
     */
    public static double rate3 = 0.10;
    /**
     * 4出现的概率为%4
     */
    public static double rate4 = 0.04;
    /**
     * 5出现的概率为%1
     */
    public static double rate5 = 0.01;

    // String 可以为任意类型 也可以自定义类型
    static Map<String, Integer> keyChanceMap = new HashMap<>();

    static {
        keyChanceMap.put("10", 13);
        keyChanceMap.put("12", 14);
        keyChanceMap.put("14", 15);
        keyChanceMap.put("13", 20);
        keyChanceMap.put("11", 17);
        keyChanceMap.put("16", 16);
    }

    public static void main1(String[] args) {
        Map<String, Integer> count = new HashMap<String, Integer>();
        List<String> list = new ArrayList<>();
        String item = null;
        for (int i = 0; i < 1; i++) {
            item = chanceSelect(keyChanceMap);
            System.out.println("===============：  " + item);
            list.add(item);

            if (count.containsKey(item)) {
                count.put(item, count.get(item) + 1);
            } else {
                count.put(item, 1);
            }

        }
        for (String id : count.keySet()) {
            System.out.println(id + "\t出现了 " + count.get(id) + " 次");
        }

        Random rand = new Random();
        int num = rand.nextInt(1);
        System.out.print("最终选择的随机数为：" + list.get(num));
    }

    public static String chanceSelect(Map<String, Integer> keyChanceMap) {
        if (keyChanceMap == null || keyChanceMap.size() == 0)
            return null;

        Integer sum = 0;
        for (Integer value : keyChanceMap.values()) {
            sum += value;
        }
        // 从1开始
        Integer rand = new Random().nextInt(sum) + 1;

        for (Map.Entry<String, Integer> entry : keyChanceMap.entrySet()) {
            rand -= entry.getValue();
            // 选中
            if (rand <= 0) {
                String item = entry.getKey();
                return item;
            }
        }
        return null;
    }

    /**
     * 充实机制工具类
     *
     * @param args
     */
    public static void main3(String[] args) {
        retryer();
    }

    /**
     * 重试实例
     */
    public static void retryer() {
        Retryer.<String>newInstance()
                .maxAttempt(5)
                .listen(RetryListens.noListen())
                .recover(Recovers.noRecover())
                .condition(RetryConditions.hasExceptionCause())
                .retryWaitContext(RetryWaiter.<String>retryWait(NoRetryWait.class).context())
                .callable(new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        System.out.println("called...");
                        Thread.sleep(1000);
                        throw new RuntimeException();
                    }
                }).retryCall();
    }


    /**
     * 折扣计算
     *
     * @param args
     */
    public static void main(String[] args) {
        // while(true) {
        System.out.println("请输入商品总价：");
        Scanner sc = new Scanner(System.in);
        double m = sc.nextDouble();
        double m1 = m;
        if (m >= 0 && m < 1000) {
            System.out.println("商品总价:" + m + "元\n" + "折扣：0.00\n应付：" + m + "元");

        } else if (m >= 1000 && m < 2000) {
            m1 = m * 0.9;
            System.out.println("商品总价:" + m + "元\n" + "折扣：-" + 0.1 * m + "元(九折)\n应付：" + m1 + "元");
        } else if (m >= 2000 && m < 5000) {
            m1 = m * 0.8;
            System.out.println("商品总价:" + m + "元\n" + "折扣：-" + 0.2 * m + "元(八折)\n应付：" + m1 + "元");
        } else if (m >= 5000) {
            m1 = m * 0.5;
            System.out.println("商品总价:" + m + "元\n" + "折扣：-" + 0.5 * m + "元(五折)\n应付：" + m1 + "元");
        } else {
            System.out.println("请输入正确的总价！\n");
        }

        sc.close();

    }
}

